/*
 * @Description:
 * @Author: 胡涛
 * @Date: 2024-07-15 21:57:30
 * @LastEditors: 胡涛
 * @LastEditTime: 2024-07-20 22:37:33
 */
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from "path";
import { fileURLToPath } from "url";
import monacoEditorPlugin from "vite-plugin-monaco-editor";

const baseSrc = fileURLToPath(new URL("./src", import.meta.url));
// export const proxyUrl = "http://162.14.65.101:3020";
// export const proxyUrl = "http://162.14.65.101:3017";
export const proxyUrl = "https://localhost:44359";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: "./",
    plugins: [vue(), vueJsx(), monacoEditorPlugin({})],
    define: {
      "process.env": {},
    },
    resolve: {
      alias: [
        {
          find: "dayjs",
          replacement: "dayjs/esm",
        },
        {
          find: /^dayjs\/locale/,
          replacement: "dayjs/esm/locale",
        },
        {
          find: /^dayjs\/plugin/,
          replacement: "dayjs/esm/plugin",
        },
        {
          find: /^ant-design-vue\/es$/,
          replacement: "ant-design-vue/es",
        },
        {
          find: /^ant-design-vue\/dist$/,
          replacement: "ant-design-vue/dist",
        },
        {
          find: /^ant-design-vue\/lib$/,
          replacement: "ant-design-vue/es",
        },
        {
          find: /^ant-design-vue$/,
          replacement: "ant-design-vue/es",
        },
        {
          find: "~@",
          replacement: baseSrc,
        },
        {
          find: "~",
          replacement: baseSrc,
        },
        {
          find: "@",
          replacement: baseSrc,
        },
      ],
    },
    build: {
      outDir: "dist",
      assetsDir: "assets", //指定静态资源存放路径
      sourcemap: false, //是否构建source map 文件
      chunkSizeWarningLimit: 4096,
      rollupOptions: {
        input: {
          index: resolve("index.html"),
        },
        output: {
          manualChunks: {
            vue: ["vue", "vue-router", "pinia"],
            antd: ["ant-design-vue", "@ant-design/icons-vue", "dayjs"],
          },
          entryFilesNames: (chunkInfo) => {
            return `assets/[name].[hash].js`;
          },
        },
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${resolve(
              "src/assets/less/index.less"
            )}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
    server: {
      host: "localhost",
      port: 3002,
      proxy: {
        "/api": {
          target: proxyUrl,
          changeOrigin: true,
          secure: false,
          ws: true,
        },
        "/connect": {
          target: proxyUrl,
          changeOrigin: true,
          secure: false,
        },
      },
    },
    preview: {
      host: "localhost",
      port: 3005,
    },
  };
});
